package com.qlslylq.ad.sdk.model.rsp;

import android.os.Build;

import com.qlslylq.ad.sdk.enums.AdPlatformEnum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 媒体<br/>
 */
public class MediaWebVO implements Serializable {

    //没有配置该平台
    public static final String NOT_CONFIG = "-1";

    private long mediaId;

    private List<MediaWebConfigureVO> configures;

    //本地辅助
    private Map<String, MediaWebConfigureVO> configureDic;

    public long getMediaId() {
        return mediaId;
    }

    public void setMediaId(long mediaId) {
        this.mediaId = mediaId;
    }

    public List<MediaWebConfigureVO> getConfigures() {
        if (configures == null) {
            configures = new ArrayList<>();
        }
        return configures;
    }

    public void setConfigures(List<MediaWebConfigureVO> configures) {
        this.configures = configures;
    }

    /**
     * 获取媒体方在第三方平台上注册的appId<br/>
     */
    private String getAppIdByPlatformBasedList(AdPlatformEnum platform) {
        for (MediaWebConfigureVO item : getConfigures()) {
            if (platform.getName().equals(item.getPlatformSign())) {
                return item.getAppId();
            }
        }
        return NOT_CONFIG;
    }

    /**
     * 获取媒体方在第三方平台上注册的appKey<br/>
     */
    private String getAppKeyByPlatformBasedList(AdPlatformEnum platform) {
        for (MediaWebConfigureVO item : getConfigures()) {
            if (platform.getName().equals(item.getPlatformSign())) {
                return item.getAppKey();
            }
        }
        return NOT_CONFIG;
    }

    /**
     * 获取媒体方在第三方平台上注册的appId<br/>
     */
    public String getAppIdByPlatform(AdPlatformEnum platform) {
        if (configureDic == null) {
            configureDic = listToMap(getConfigures());
        }
        if (configureDic != null) {
            MediaWebConfigureVO item = configureDic.get(platform.getName());
            return item == null ? NOT_CONFIG : item.getAppId();
        } else {
            return getAppIdByPlatformBasedList(platform);
        }
    }

    /**
     * 获取媒体方在第三方平台上注册的appKey<br/>
     */
    public String getAppKeyByPlatform(AdPlatformEnum platform) {
        if (configureDic == null) {
            configureDic = listToMap(getConfigures());
        }
        if (configureDic != null) {
            MediaWebConfigureVO item = configureDic.get(platform.getName());
            return item == null ? NOT_CONFIG : item.getAppKey();
        } else {
            return getAppKeyByPlatformBasedList(platform);
        }
    }

    public Map<String, MediaWebConfigureVO> listToMap(List<MediaWebConfigureVO> list) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return list.stream().collect(Collectors.toMap(MediaWebConfigureVO::getPlatformSign, Function.identity(),
                    (v1, v2) -> v2, HashMap::new));
        }
        return null;
    }

}
